package com.another.bidservice.service.impl;

import com.another.bidservice.entity.TCodeRule;
import com.another.bidservice.entity.TCompany;
import com.another.bidservice.dao.jpa.CompanyJPADao;
import com.another.bidservice.dao.mapper.CompanyDao;
import com.another.bidservice.service.ICompanyService;
import com.another.common.enums.CodeRuleEnum;
import com.another.common.enums.Enums;
import com.another.common.utils.common.DateUtil;
import com.another.common.utils.common.EmptyUtil;
import com.another.common.utils.common.MyException;
import com.another.common.utils.result.Result;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class CompanyServiceImpl extends BaseServiceImpl implements ICompanyService {

    private Logger logger=Logger.getLogger(this.getClass());
    
    @Autowired
    private CompanyDao companyDao;
    
    @Autowired
    private CompanyJPADao companyJPADao;
    
    @Override
    public Result getList(TCompany tCompany) {
        String sql="select * from db_sys.vt_company";
        List<TCompany> list=companyDao.getList(sql);
        if (EmptyUtil.isEmpty(list)) {
            return Result.success("没有查询到数据！", "");
        } else {
            return Result.success(list);
        }
    }

    
    @Override
    public Result insertList(List<TCompany> tCompanys) {
        int ret = companyDao.insertList(tCompanys);
        if(ret==tCompanys.size()){
            return Result.success("添加成功！","");
        }else{
            throw new MyException(101, "添加失败！");
        }
    }

    
    @Override
    public Result deleteList(List<Integer> rowid) {
        int ret = companyDao.deleteList(rowid);
        if(ret==rowid.size()){
            return Result.success("删除成功！","");
        }else{
            throw new MyException(101, "删除失败！");
        }
    }
    
    @Override
    public Result updateList(List<TCompany> tCompanys) {
        try {
            int ret = companyDao.updateList(tCompanys);
            return Result.success("修改成功！","");
        }catch (Exception e){
            logger.info(e.getMessage(),e);
            throw new MyException(101, "修改失败！");
        }
    }
    
    
    @Override
    public Result insertCompany(TCompany tCompany) {
        try {

            if (EmptyUtil.isEmpty(tCompany.getCompanyName())) {
                return Result.error("请设置公司名称！");
            }

            if (EmptyUtil.isEmpty(tCompany.getCompanyType())) {
                return Result.error("请设置公司类型！");
            }

            //查看是否有重复公司
            List<TCompany> list = companyJPADao.findAllByCompanyNameAndCompanyStatus(tCompany.getCompanyName(), Enums.CompanyStatus.ON.getCode());

            if (EmptyUtil.isNotEmpty(list)) {
                return Result.error("公司名称已存在！");
            }

            //生成公司编号
            String companyId = generateNumber(CodeRuleEnum.COMPANY_ID.getCode());
            tCompany.setCompanyId(companyId);
            tCompany.setCompanyStatus(Enums.CompanyStatus.ON.getCode());
            tCompany.setCreateId(tCompany.getRequestUserId());
            tCompany.setCreateDate(DateUtil.getNowDateByyyyyMMddHHmmss());
            tCompany = companyJPADao.save(tCompany);
            return Result.success("添加成功！", tCompany);
        } catch (Exception e) {
            logger.info(e.getMessage(),e);
            throw new MyException(101, "添加失败！");
        }
    }

    @Override
    public Result updateCompany(TCompany tCompany) {
        try {

            if (EmptyUtil.isEmpty(tCompany.getCompanyId())) {
                return Result.error("公司编号没传");
            }

            //如果修改了公司名称 查看修改后的名称是否存在
            if (EmptyUtil.isNotEmpty(tCompany.getCompanyName())) {
                List<TCompany> list = companyJPADao.findAllByCompanyIdIsNotAndCompanyNameIsAndCompanyStatusIs(tCompany.getCompanyId(), tCompany.getCompanyName(), Enums.CompanyStatus.ON.getCode());
                if (EmptyUtil.isNotEmpty(list)) {
                    return Result.error("公司名已存在");
                }
            }

            tCompany.setUpdateId(tCompany.getRequestUserId());

            int ret = companyDao.updateCompany(tCompany);
            return Result.success("修改成功！");
        } catch (Exception e) {
            logger.info(e.getMessage(),e);
            throw new MyException(101, "修改失败！");
        }
    }

    @Override
    public Result deleteCompany(TCompany tCompany) {
        try {
            int ret = companyDao.deleteCompany(tCompany);
            return Result.success("删除成功！");
        } catch (Exception e) {
            logger.info(e.getMessage(),e);
            throw new MyException(101, "删除失败！");
        }
    }
}



